# include <iostream>
# include <cstring>
# include <cstdio>
int board[10][10];
bool inboard(int x, int y)
{
    return 1<=x&&x<=8 && 1<=y&&y<=8;
}
const int dx[] = {0,0,-1,1};
const int dy[] = {-1,1,0,0};
const int ddx[] = {1,1,-1,-1};
const int ddy[] = {-1,1,1,-1};
const int dnx[] = {2,2,-2,-2,1,1,-1,-1};
const int dny[] = {1,-1,1,-1,2,-2,2,-2};
int main()
{
    freopen("D-large.in", "r", stdin);
    freopen("D-large.txt", "w", stdout);
    int T, n;
    std::cin >> T;
    for (int ica = 1; ica <= T; ++ica) {
        std::cin >> n;
        memset(board, 0, sizeof(board));
        char pos[7];
        for (int i = 0; i < n; ++i) {
            std::cin >> pos;
            int x = pos[0] - 'A' + 1;
            int y = pos[1] - '1' + 1;
            board[x][y] = pos[3];
        }
        int sum = 0;
        for (int i = 1; i <= 8; ++i) {
            for (int j = 1; j <= 8; ++j) {
                if (!board[i][j]) continue;
                if (board[i][j] == 'K') {
                   // std::cout << "King : " << sum << std::endl;
                    for (int ii = -1; ii <= 1; ++ii) {
                        for (int jj = -1; jj <= 1; ++jj) {
                            if (!ii && !jj) continue;
                            int ni = i + ii;
                            int nj = j + jj;
                            if (inboard(ni,nj) && board[ni][nj]) ++sum;
                        }
                    }
                  //  std::cout << "King : " << sum << std::endl;
                } else if (board[i][j] == 'Q') {
                 //   std::cout << "Quenen" << sum << std::endl;
                    for (int ii = -1; ii <= 1; ++ii) {
                        for (int jj = -1; jj <= 1; ++jj) {
                            if (!ii && !jj) continue;
                            for (int k = 0; k <= 8; ++k) {
                                int ni = i + ii*k;
                                int nj = j + jj*k;
                                if (i==ni && j==nj) continue;
                                if (inboard(ni,nj) && board[ni][nj]) {if (board[ni][nj]!='Q') ++sum; break;}
                            }
                        }
                    }
                 //   std::cout << "Quenen" << sum << std::endl;
                } else if (board[i][j] == 'R') {
                 //   std::cout << "R" << sum << std::endl;
                    for (int ii = 0; ii < 4; ++ii) {
                        for (int k = 0; k <= 8; ++k) {
                            int ni = i + dx[ii] * k;
                            int nj = j + dy[ii] * k;
                            if (i==ni && j==nj) continue;
                            if (inboard(ni, nj) && board[ni][nj]) {if (board[ni][nj]!='R') ++sum; break;}
                        }
                    }
                 //   std::cout << "R" << sum << std::endl;
                } else if (board[i][j] == 'B') {
                 //   std::cout << "B" << sum << std::endl;
                    for (int ii = 0; ii < 4; ++ii) {
                        for (int k = 0; k <= 8; ++k) {
                            int ni = i + ddx[ii] * k;
                            int nj = j + ddy[ii] * k;
                            if (i==ni && j==nj) continue;
                            if (inboard(ni,nj) && board[ni][nj]) {if (board[ni][nj]!='B') ++sum; break;}
                        }
                    }
                 //   std::cout << "B" << sum << std::endl;
                } else if (board[i][j] == 'N') {
                 //   std::cout << "N" << sum << std::endl;
                    for (int ii = 0; ii < 8; ++ii) {
                        int ni = i + dnx[ii];
                        int nj = j + dny[ii];
                        if (inboard(ni,nj) && board[ni][nj]) ++sum;
                    }
                 //   std::cout << "N" << sum << std::endl;
                } else if (board[i][j] = 'P') {
                 //   std::cout << "P" << sum << std::endl;
                    for (int ii = 0; ii < 4; ++ii) {
                        if (ddx[ii] < 0) continue;
                        int ni = i + ddx[ii];
                        int nj = j + ddy[ii];
                        if (inboard(ni,nj) && board[ni][nj]) ++sum;
                    }
                 //   std::cout << "P" << sum << std::endl;
                }
            }
        }
        std::cout << "Case #" << ica << ": "<<sum<<std::endl;
    }
    return 0;
}
